export default class WsPlayer {
  constructor(options) {
    if (typeof options.Mse !== 'object') {
      console.log('MSE is not a object')
    }
    if (typeof options.Drawer !== 'object') {
      console.log('drawer is not a object')
    }
    this.mse = options.Mse
    this.drawer = options.Drawer
    this.frames = 0
    this.buffer = []
  }
  openWs(url, id, name, socketUrl = '/ws/websocket/socketServer.do') {
    return new Promise((resolve, reject) => {
      if (!url.startsWith('ws://')) {
        url = 'ws://' + url + socketUrl
      }
      let self = this
      let ws = new WebSocket(url)
      self.ws = ws
      self.id = id
      ws.binaryType = 'arraybuffer'
      ws.onopen = function() {
        console.log('-- WebSocket is opened! --')
        console.log(`-- LISTEN ip : ${id}`)
        var param = {
          cmd: 'ip',
          data: self.id
        }
        ws.send(JSON.stringify(param))
        resolve()
      }
      ws.onerror = function (err) {
        reject(err)
      }
      ws.onmessage = function (event) {
        // console.log('接收数据', event.data)
        if (Object.prototype.toString.call(event.data) === '[object ArrayBuffer]') {
          self.mse.appendBuffer(event.data)
          // self.drawer.receive(event.data)
          let array = Array.prototype.slice.call(new Uint8Array(event.data))
          self.buffer.push.apply(self.buffer, array)
          self.frames ++
          if (self.frames > 3) {
            var file = new Uint8Array(self.buffer).buffer
            self.drawer.receive(file)
            self.frames = 0
            self.buffer.length = 0
          }
        }
      }
    })
  }
  play() {
    // this.ws && this.ws.send('LISTEN:' + this.id)
    this.drawer.startAnimation()
    this.mse.play()
  }
  closeVoice() {
    this.mse.pause()
  }
  openVoice() {
    this.mse.play()
  }
  pause() {
    this.mse.pause()
    this.drawer.stopAnimation()
    this.ws && this.ws.send('STOP_LISTEN')
  }
  stop(id) {
    // stop animation
    this.drawer.stopAnimation()
    return new Promise((resolve, reject) => {
      if (Object.prototype.toString.call(this.ws) === '[object WebSocket]') {
        var stopParam = {
          cmd: 'STOP_LISTEN',
          data: id
        }
        this.ws.send(JSON.stringify(stopParam))
        this.ws.onclose = function () {
          resolve(1)
        }
        this.mse && this.mse.pause()
        this.ws.close()
        this.ws = null
      } else {
        resolve(0)
      }
    })
  }
}